package com.mt.controller;


import com.mt.pojo.OrderInfo;
import com.mt.service.OrderService;
import com.mt.constants.Result;
import com.mt.pojo.Ticket;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Wang
 * @since 2020-12-28
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    OrderService orderService;


    @GetMapping()
    public Result getOrderList(@RequestParam("userId") String userId){
        Result<Object> result = new Result<>();
        result.success(orderService.listOrderByUser(userId));
        return result;
    }

    @GetMapping("/check/{orderId}")
    public Result checkOrdersStatus(@PathVariable("orderId") String orderId){
        Result<Object> result = new Result<>();
        result.success(orderService.checkOrdersStatus(orderId));
        return result;
    }

    @GetMapping("/confirmPay")
    public Result confirmPayOrder(@RequestParam("orderId") String orderId){
        Result<Object> result = new Result<>();
        result.success(orderService.confirmPayOrder(orderId));
        return result;
    }


    @GetMapping("/page")
    public Result pageOrderList(@RequestParam("currentPage")int currentPage,
                                @RequestParam("pageSize")int pageSize,
                                @RequestParam("filmName") String filmName,
                                @RequestParam("cinemaId") int cinemaId){
        Result<Object> result = new Result<>();
        result.success(orderService.pageOrderList(currentPage,pageSize,filmName,cinemaId));
        return result;
    }

    @PostMapping()
    public  Result createOrder(@RequestBody Ticket ticket,@RequestParam("userId")String userId){
        ticket.setUserId(userId);
        Result<Object> result = new Result<>();
        result.success(orderService.createOrder(ticket.getUserId(),ticket.getScheduleId(),ticket.getSeatList()));
        return result;
    }

    @GetMapping("/sold")
    public Result getSoldSeatIdList(@RequestParam("scheduleId") Integer scheduleId){
        Result<Object> result = new Result<>();
        result.success(orderService.getSoldTickets(scheduleId));
        return result;
    }
    @PostMapping("/cancel")
    public Result cancelOrder(@RequestBody OrderInfo orderInfo){
        Result<Object> result = new Result<>();
        result.success(orderService.cancelOrder(orderInfo.getOrderId()));
        return result;
    }
}
